package cn.lu.train.view;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Rect;
import android.graphics.drawable.Drawable;
import android.support.annotation.Nullable;
import android.util.AttributeSet;

import cn.lu.train.R;

public class MyCheckBox extends android.support.v7.widget.AppCompatCheckBox {

    private int leftWidth;
    private int leftHeight;
    private int rightWidth;
    private int rightHeight;
    private int topWidth;
    private int topHeight;
    private int bottomWidth;
    private int bottomHeight;

    private Drawable[] mRectDrawables;

    public MyCheckBox(Context context) {
        super(context);
    }

    public MyCheckBox(Context context, AttributeSet attrs) {
        super(context, attrs);
        initDrawableSize(context, attrs);
    }

    public MyCheckBox(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initDrawableSize(context, attrs);
    }

    private void initDrawableSize(Context context, AttributeSet attrs) {
        TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.MyCheckBox);
        leftWidth = ta.getDimensionPixelSize(R.styleable.MyCheckBox_left_drawable_width, 0);
        leftHeight = ta.getDimensionPixelSize(R.styleable.MyCheckBox_left_drawable_height, 0);

        rightWidth = ta.getDimensionPixelSize(R.styleable.MyCheckBox_right_drawable_width, 0);
        rightHeight = ta.getDimensionPixelSize(R.styleable.MyCheckBox_right_drawable_height, 0);

        topWidth = ta.getDimensionPixelSize(R.styleable.MyCheckBox_bottom_drawable_width, 0);
        topHeight = ta.getDimensionPixelSize(R.styleable.MyCheckBox_top_drawable_height, 0);

        bottomWidth = ta.getDimensionPixelSize(R.styleable.MyCheckBox_bottom_drawable_width, 0);
        bottomHeight = ta.getDimensionPixelSize(R.styleable.MyCheckBox_bottom_drawable_height, 0);


        ta.recycle();
        setRectDrawableForCheckBox(mRectDrawables[0], mRectDrawables[2], mRectDrawables[1], mRectDrawables[3]);
    }

    @Override
    public void toggle() {
        //super.toggle();
    }

    @Override
    public void setCompoundDrawables(@Nullable Drawable left, @Nullable Drawable top, @Nullable Drawable right, @Nullable Drawable bottom) {
        if (mRectDrawables == null) {
            mRectDrawables = new Drawable[4];
            mRectDrawables[0] = left;
            mRectDrawables[1] = right;
            mRectDrawables[2] = top;
            mRectDrawables[3] = bottom;
        } else {
            setRectDrawableForCheckBox(left, top, right, bottom);
        }

    }

    private void setRectDrawableForCheckBox(Drawable left, Drawable top, Drawable right, Drawable bottom) {
        if ((leftWidth > 0 || leftHeight > 0) && left != null) {
            Rect bound = left.getBounds();
            left.setBounds(bound.left,bound.top, leftWidth > 0 ? leftWidth : bound.right, leftHeight > 0 ? leftHeight : bound.bottom);
        }

        if ((rightWidth > 0 || rightHeight > 0) && left != null) {
            Rect bound = right.getBounds();
            right.setBounds(bound.left,bound.top, rightWidth > 0 ? rightWidth : bound.right, rightHeight > 0 ? rightHeight : bound.bottom);
        }

        if ((topWidth > 0 || topHeight > 0) && left != null) {
            Rect bound = top.getBounds();
            top.setBounds(bound.left,bound.top, topWidth > 0 ? topWidth : bound.right, topHeight > 0 ? topHeight : bound.bottom);
        }

        if ((bottomWidth > 0 || bottomHeight > 0) && left != null) {
            Rect bound = bottom.getBounds();
            bottom.setBounds(bound.left,bound.top, bottomWidth > 0 ? bottomWidth : bound.right, bottomHeight > 0 ? bottomHeight : bound.bottom);
        }

        super.setCompoundDrawables(left, top, right, bottom);
    }
}
